package com.jxt.myjxtmanager.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.ui.fragment.LeaveForAnswerFragment;
import com.jxt.myjxtmanager.ui.fragment.LeaveForAskFragment;

/**
 * Created by qm on 2017/3/10 0010.
 */
public class AskForLeaveActivity extends BaseActivity {

    private LeaveForAskFragment askFragment;
    private Button btnAsk;
    private Button btnAnswer;
    private static final String TAG_ask = "ask";
    private static final String TAG_answer = "answer";
    private String currentFragTag = TAG_ask;// 当前fragment的tag
    /**
     * 用于对Fragment进行管理
     */
    public FragmentManager fragmentManager;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_ask_for_leave;
    }

    @Override
    protected void initView() {
        setToolbarTitle("在线批假");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        btnAsk = (Button) findViewById(R.id.btn_ask_for_leave);
        btnAnswer = (Button) findViewById(R.id.btn_answer_for_leave);

        btnAsk.setOnClickListener(this);
        btnAnswer.setOnClickListener(this);
    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    protected void initData() {
        askFragment = new LeaveForAskFragment();
        fragmentManager = getSupportFragmentManager();
        // 第一次启动时选中TAG_info
        fragmentManager.beginTransaction()
                .replace(R.id.off_map_frame, askFragment, TAG_ask).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ask_for_leave:
                switchFrag(currentFragTag, TAG_ask);
                break;
            case R.id.btn_answer_for_leave:
                switchFrag(currentFragTag, TAG_answer);
                break;
        }
    }

    enum FragTags {
        ask, answer;
    }

    private void switchFrag(String preFrag, String fragTag) {
        if (!preFrag.equals(fragTag)) {
            switchImg(fragTag);// 顶部导航图片高亮显示
            // 查找fragment栈，看是否该fragment已存在，存在show,不存在则新建
            Fragment tempFragment = fragmentManager.findFragmentByTag(fragTag);
            Fragment preFragment = fragmentManager.findFragmentByTag(preFrag);
            if (tempFragment == null) {
                FragTags tag = FragTags.valueOf(fragTag);
                String tagStr = "";
                Fragment storeFrag = null;
                switch (tag) {
                    case ask:
                        tagStr = TAG_ask;
                        storeFrag = new LeaveForAskFragment();
                        break;
                    case answer:

                        tagStr = TAG_answer;
                        storeFrag = new LeaveForAnswerFragment();
                        break;

                    default:
                        break;
                }
                fragmentManager.beginTransaction().hide(preFragment)
                        .add(R.id.off_map_frame, storeFrag, tagStr).commit();

            } else {
                fragmentManager.beginTransaction().hide(preFragment)
                        .show(tempFragment).commit();
            }
        }
        currentFragTag = fragTag;
    }

    // 底部TextView切换高亮
    private void switchImg(String fargTag) {
        FragTags currenttag = FragTags.valueOf(currentFragTag);
        switch (currenttag) {
            case ask:
                btnAsk.setTextColor(getResources().getColor(R.color.txt_bule_color));
                btnAsk.setBackgroundResource(R.drawable.btbg_circle_normal);
                break;
            case answer:
                btnAnswer.setTextColor(getResources().getColor(R.color.txt_bule_color));
                btnAnswer.setBackgroundResource(R.drawable.btbg_circle_normal);
                break;
        }

        FragTags tag = FragTags.valueOf(fargTag);
        switch (tag) {
            case ask:
                btnAsk.setTextColor(getResources().getColor(R.color.white));
                btnAsk.setBackgroundResource(R.drawable.btbg_circle_press);
                break;
            case answer:
                btnAnswer.setTextColor(getResources().getColor(R.color.white));
                btnAnswer.setBackgroundResource(R.drawable.btbg_circle_press);
                break;
            default:
                break;
        }
    }


}
